package com.wuseguang.springcloud.controller;


import com.wuseguang.springcloud.Classcardservice.ClassCardService;
import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.service.ClassVideoService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 班级视频表 前端控制器
 * </p>
 *
 * @author
 * @since 2020-01-07
 */
@RestController
@RequestMapping("/classVideo")
public class ClassVideoController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ClassVideoService classVideoServiceImpl;

    //推送接口
    @Autowired
    private ClassCardService classCardService;

    /**
     * 添加班级视屏
     *
     * @param request
     * @param name       名称/标题
     * @param show       是否展示在班牌首页  0不显示，1显示
     * @param classId    班级id
     * @param videoUrl   视频URL
     * @param videoCover 视频封面
     * @return
     */
    @RequestMapping("/addClassVideo")
    public Result addClassVideo(RequestParameterWrapper request,
                                @RequestParam(value = "name", required = false) String name,
                                @RequestParam(value = "show", required = false, defaultValue = "0") Integer show,
                                @RequestParam(value = "pshow", required = false, defaultValue = "0") Integer pshow,
                                @RequestParam(value = "classId", required = false) Integer[] classId,
                                @RequestParam(value = "videoUrl", required = false) String videoUrl,
                                @RequestParam(value = "videoCover", required = false) String videoCover) {
        logger.info("添加班级视频 ClassVideoController - > addClassVideo");

        //判断视频名称是否为空
        /*if (StringUtils.isEmpty(name)) {
            return new Result(CodeUtil.REQUIRED_VALUE_IS_EMPTY.getCode(), CodeUtil.REQUIRED_VALUE_IS_EMPTY.getMsg() + "[视频标题为空]");
        }
        if (StringUtils.isEmpty(videoCover)) {
            return new Result(CodeUtil.REQUIRED_VALUE_IS_EMPTY.getCode(), CodeUtil.REQUIRED_VALUE_IS_EMPTY.getMsg() + "[视频封面为空]");
        }*/
        //判断是否上传了视频
        if (StringUtils.isEmpty(videoUrl)) {
            return new Result(CodeUtil.NO_VIDEO_UPLOADED);
        }

        //学校id
        String schoolId = request.getParam("schoolId");
        //教师id
        String teacherId = request.getParam("teacherId");
        //教师名称
        String teacherName = request.getParam("teacherName");
        //学期id
        String termId = request.getParam("termId");

        Result result = classVideoServiceImpl.addClassVideo(schoolId, name, show,pshow, classId, videoUrl, termId, teacherName, teacherId, videoCover);
        if (classId != null && classId.length > 0) {
            List<Integer> integers = Arrays.asList(classId);
            classCardService.classesAndCards(integers);
        }
        return result;
    }

    /**
     * 置顶视频
     *
     * @param request
     * @param id      视频id
     * @return
     */
    @RequestMapping("/roofPlacementVideo")
    public Result roofPlacementVideo(RequestParameterWrapper request,
                                     @RequestParam(value = "id", required = false) String id) {
        logger.info("视频置顶到班级 ClassVideoController - > roofPlacementVideo");
        //教师id
        String teacherId = request.getParam("teacherId");
        //获取是不是管理员
        String admin = request.getParam("admin");
        //学校id
        String schoolId = request.getParam("schoolId");

        return classVideoServiceImpl.roofPlacementVideo(id, teacherId, admin, schoolId);
    }

    /**
     * 删除视频
     *
     * @param request
     * @param id      视频id
     * @return
     */
    @RequestMapping("/deleteVideo")
    public Result deleteVideo(RequestParameterWrapper request,
                              @RequestParam(value = "id", required = false) String id) {
        //教师id
        String teacherId = request.getParam("teacherId");
        //获取是不是管理员
        String admin = request.getParam("admin");
        logger.info("删除视频  id = {}", id);
        return classVideoServiceImpl.deleteVideo(id, teacherId, admin);
    }

    /**
     * 查询视频信息
     *
     * @param request
     * @param classId
     * @param indexNo
     * @return
     */
    @RequestMapping("/findClassVideoList")
    public Result findClassVideoList(RequestParameterWrapper request,
                                     @RequestParam(value = "classId", required = false) String[] classId,
                                     @RequestParam(value = "indexNo", required = false, defaultValue = "1") Integer indexNo,
                                     @RequestParam(value = "rows", required = false, defaultValue = "20") Integer rows) {
        logger.info("查询视频信息 ClassVideoController - > findClassVideoList");

        //学期id
        String termId = request.getParam("termId");
        //学校id
        String schoolId = request.getParam("schoolId");
        //管理员
        String admin = request.getParam("admin");
        //教师id
        String teacherId = request.getParam("teacherId");

        return classVideoServiceImpl.findClassVideoList(classId, termId, schoolId, indexNo, rows, admin, teacherId);
    }
}

